package com.sitech.openapi.componet;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.sitech.openapi.componet.ObjectTree.TreeNode;
public class ObjTreeCaseXml {

    private TreeNode node;

    public ObjTreeCaseXml() {
    }

    public ObjTreeCaseXml(TreeNode node) {
        this.node = node;
    }

    public String toXML() {
        StringBuffer sb = new StringBuffer();
        this.marshaller(sb, this.node);
        return sb.toString();
    }


    /**
     * 解析MapBean
     * @param sb 解析结果
     * @param node MapBean节点
     */
    private void marshaller(StringBuffer sb, TreeNode node) {
        Map cmap = node.getIterator();

        Iterator it = cmap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            Object value = entry.getValue();

            if (value instanceof TreeNode) {
                TreeNode p_node = (TreeNode) value;
                if (p_node.Iterator.size() != 0) {
                    sb.append("<").append(key).append(getAttrXML(p_node.getNodeAttr())).append(">");
                    marshaller(sb, p_node);
                    sb.append("</").append(key).append(">");
                } else {
                    if (p_node.getNodeValue() == null || ("").equals(p_node.getNodeValue())) {
                        sb.append("<").append(key).append(getAttrXML(p_node.getNodeAttr())).append("/>");
                    } else {
                        sb.append("<").append(key).append(getAttrXML(p_node.getNodeAttr())).append(">")
                                .append(formatValue(p_node.getNodeValue())).append("</").append(key)
                                .append(">");
                    }
                }
            }

            if (value instanceof List) {
                List p_list = (List) value;
                for (Iterator list_it = p_list.iterator(); list_it.hasNext();) {
                    Object list_value = list_it.next();
                    if (list_value instanceof TreeNode) {
                        TreeNode p_node = (TreeNode) list_value;
                        if (p_node.Iterator.size() != 0) {
                            sb.append("<").append(key).append(getAttrXML(p_node.getNodeAttr()))
                                    .append(">");
                            marshaller(sb, p_node);
                            sb.append("</").append(key).append(">");
                        } else {
                            if (p_node.getNodeValue() == null || ("").equals(p_node.getNodeValue())) {
                                sb.append("<").append(key).append(getAttrXML(p_node.getNodeAttr())).append("/>");
                            } else {
                                sb.append("<").append(key).append(getAttrXML(p_node.getNodeAttr()))
                                        .append(">").append(formatValue(p_node.getNodeValue()))
                                        .append("</").append(key).append(">");
                            }
                        }
                    }
                }
            }

        }
    }


    /**
     * 属性拼接转换
     * @param attrMap 属性map
     * @return string 属性string
     */
    private String getAttrXML(Map attrMap) {
        if (attrMap == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Iterator it = attrMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> attrE = (Map.Entry<String, String>) it.next();
            String attrKey = attrE.getKey();
            String attrValue = attrE.getValue();
            sb.append(" " + attrKey).append("=").append("\"").append(attrValue).append("\" ");
        }
        return sb.toString();
    }


    private String formatValue(Object obj) {

        return String.valueOf(obj).replaceAll("&", "&amp;").replaceAll("<", "&lt;")
                .replaceAll(">", "&gt;").replaceAll("'", "&apos;").replaceAll("\"", "&quot;");

    }

}
